package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.air.info.SupplierSystemOrderAirFlightInfoDTO;
import com.ctshk.rpc.booking.dto.system.air.info.SupplierSystemOrderAirInfoDTO;
import com.ctshk.rpc.booking.dto.system.air.info.SupplierSystemOrderAirTicketInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAir;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAirFlight;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAirTicket;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderAirMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderAirFlightMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderAirTicketMapper;
import com.ctshk.rpc.booking.req.system.air.add.SupplierSystemOrderAirAddReq;
import com.ctshk.rpc.booking.req.system.air.add.SupplierSystemOrderAirFlightAddReq;
import com.ctshk.rpc.booking.req.system.air.add.SupplierSystemOrderAirTicketAddReq;
import com.ctshk.rpc.booking.req.system.air.edit.SupplierSystemOrderAirEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderAirService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统机票信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierSystemOrderAirServiceImpl implements ISupplierSystemOrderAirService {
    @Autowired
    SupplierSystemOrderAirMapper supplierSystemOrderAirMapper;
    @Autowired
    SupplierSystemOrderAirFlightMapper supplierSystemOrderAirFlightMapper;
    @Autowired
    SupplierSystemOrderAirTicketMapper supplierSystemOrderAirTicketMapper;

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderAirEditReq> supplierSystemOrderAirEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderAirEditReqList)) {
            supplierSystemOrderAirEditReqList.forEach(airEditReq -> {
                UpdateWrapper<SupplierSystemOrderAir> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderAir::getId, airEditReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderAir::getCustomerRemark, airEditReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderAir::getSupplierRemark, airEditReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderAir::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderAir::getGmtModified, nowDateTime);
                supplierSystemOrderAirMapper.update(new SupplierSystemOrderAir(), updateWrapper);
                airEditReq.getSupplierSystemOrderAirTicketEditReqList().forEach(airTicketReq -> {
                    UpdateWrapper<SupplierSystemOrderAirTicket> updateTicketWrapper = new UpdateWrapper<>();
                    updateTicketWrapper.lambda().eq(SupplierSystemOrderAirTicket::getId, airTicketReq.getId());
                    updateTicketWrapper.lambda().set(SupplierSystemOrderAirTicket::getSinglePrice, airTicketReq.getSinglePrice());
                    updateTicketWrapper.lambda().set(SupplierSystemOrderAirTicket::getTaxAmount, airTicketReq.getTaxAmount());
                    updateTicketWrapper.lambda().set(SupplierSystemOrderAirTicket::getTotalPrice, airTicketReq.getSinglePrice().add(airTicketReq.getTaxAmount()));
                    updateTicketWrapper.lambda().set(SupplierSystemOrderAirTicket::getModifiedId, userId);
                    updateTicketWrapper.lambda().set(SupplierSystemOrderAirTicket::getGmtModified, nowDateTime);
                    supplierSystemOrderAirTicketMapper.update(null, updateTicketWrapper);
                });
            });
        }
    }

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderAirAddReq> supplierSystemOrderAirAddReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderAirAddReqList)) {
            List<SupplierSystemOrderAir> supplierSystemOrderAirs = new ArrayList<>();
            supplierSystemOrderAirAddReqList.forEach(airAddReq -> {
                SupplierSystemOrderAir supplierSystemOrderAir = EntityUtil.copy(airAddReq, SupplierSystemOrderAir.class);
                supplierSystemOrderAir.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderAir.setOrderId(id);
                supplierSystemOrderAir.setCreateId(userId);
                supplierSystemOrderAir.setGmtCreate(nowDateTime);
                supplierSystemOrderAir.setModifiedId(userId);
                supplierSystemOrderAir.setGmtModified(nowDateTime);
                supplierSystemOrderAir.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderAirs.add(supplierSystemOrderAir);
                //添加旅客
                List<SupplierSystemOrderAirTicketAddReq> supplierSystemOrderAirTicketAddReqList = airAddReq.getSupplierSystemOrderAirTicketAddReqList();
                if (CollectionUtils.isNotEmpty(supplierSystemOrderAirTicketAddReqList)) {
                    List<SupplierSystemOrderAirTicket> supplierSystemOrderAirTickets = new ArrayList<>();
                    supplierSystemOrderAirTicketAddReqList.forEach(airTicketAdd -> {
                        SupplierSystemOrderAirTicket supplierSystemOrderAirTicket = EntityUtil.copy(airTicketAdd, SupplierSystemOrderAirTicket.class);
                        supplierSystemOrderAirTicket.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderAirTicket.setOrderId(id);
                        supplierSystemOrderAirTicket.setAirId(supplierSystemOrderAir.getId());
                        supplierSystemOrderAirTicket.setCreateId(userId);
                        supplierSystemOrderAirTicket.setGmtCreate(nowDateTime);
                        supplierSystemOrderAirTicket.setModifiedId(userId);
                        supplierSystemOrderAirTicket.setGmtModified(nowDateTime);
                        supplierSystemOrderAirTicket.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderAirTickets.add(supplierSystemOrderAirTicket);

                        /*SupplierSystemOrderAirFlightAddReq supplierSystemOrderAirFlightAddReq = airTicketAdd.getSupplierSystemOrderAirFlightAddReq();
                        if (supplierSystemOrderAirFlightAddReq != null) {
                            SupplierSystemOrderAirFlight supplierSystemOrderAirFlight = EntityUtil.copy(supplierSystemOrderAirFlightAddReq, SupplierSystemOrderAirFlight.class);
                            supplierSystemOrderAirFlight.setId(SnowflakeIdWorker.nextId());
                            supplierSystemOrderAirFlight.setOrderId(id);
                            supplierSystemOrderAirFlight.setTicketId(supplierSystemOrderAirTicket.getId());
                            supplierSystemOrderAirFlight.setCreateId(userId);
                            supplierSystemOrderAirFlight.setGmtCreate(nowDateTime);
                            supplierSystemOrderAirFlight.setModifiedId(userId);
                            supplierSystemOrderAirFlight.setGmtModified(nowDateTime);
                            supplierSystemOrderAirFlight.setIsDeleted(IsDeletedCode.NO.getCode());
                            supplierSystemOrderAirFlightMapper.insert(supplierSystemOrderAirFlight);
                        }*/
                    });
                    supplierSystemOrderAirTicketMapper.insertBatchSomeColumn(supplierSystemOrderAirTickets);
                }
            });
            supplierSystemOrderAirMapper.insertBatchSomeColumn(supplierSystemOrderAirs);
        }
    }

    @Override
    public List<SupplierSystemOrderAirInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderAir> airQueryWrapper = new QueryWrapper<>();
        airQueryWrapper.lambda().eq(SupplierSystemOrderAir::getOrderId, id);
        List<SupplierSystemOrderAir> supplierSystemOrderAirList = supplierSystemOrderAirMapper.selectList(airQueryWrapper);
        List<SupplierSystemOrderAirInfoDTO> supplierSystemOrderAirInfoDTOList = EntityUtil.copyList(supplierSystemOrderAirList, SupplierSystemOrderAirInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderAirInfoDTOList)) {
            supplierSystemOrderAirInfoDTOList.forEach(airDTO -> {
                //添加机票
                QueryWrapper<SupplierSystemOrderAirTicket> airTicketQueryWrapper = new QueryWrapper<>();
                airTicketQueryWrapper.lambda().eq(SupplierSystemOrderAirTicket::getOrderId, id);
                airTicketQueryWrapper.lambda().eq(SupplierSystemOrderAirTicket::getAirId, airDTO.getId());
                List<SupplierSystemOrderAirTicket> supplierSystemOrderAirTickets = supplierSystemOrderAirTicketMapper.selectList(airTicketQueryWrapper);
                List<SupplierSystemOrderAirTicketInfoDTO> supplierSystemOrderAirTicketInfoDTOS = EntityUtil.copyList(supplierSystemOrderAirTickets, SupplierSystemOrderAirTicketInfoDTO.class);
                airDTO.setSupplierSystemOrderAirTicketInfoDTOList(supplierSystemOrderAirTicketInfoDTOS);
                /*//添加机票航班
                if (CollectionUtils.isNotEmpty(supplierSystemOrderAirTicketInfoDTOS)) {
                    supplierSystemOrderAirTicketInfoDTOS.forEach(airTicketInfoDTO -> {
                        QueryWrapper<SupplierSystemOrderAirFlight> airFlightQueryWrapper = new QueryWrapper<>();
                        airFlightQueryWrapper.lambda().eq(SupplierSystemOrderAirFlight::getOrderId, id);
                        airFlightQueryWrapper.lambda().eq(SupplierSystemOrderAirFlight::getId, airDTO.getId());
                        SupplierSystemOrderAirFlight supplierSystemOrderAirFlight = supplierSystemOrderAirFlightMapper.selectOne(airFlightQueryWrapper);
                        airTicketInfoDTO.setSupplierSystemOrderAirFlightInfoDTO(EntityUtil.copy(supplierSystemOrderAirFlight, SupplierSystemOrderAirFlightInfoDTO.class));
                    });
                }*/
            });
        }
        return supplierSystemOrderAirInfoDTOList;
    }
}
